package cn.cdeden.knowledge.controller.schedule;

import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.SysPermissionsRelevance;
import cn.cdeden.knowledge.domain.SysScheduleDetails;
import cn.cdeden.knowledge.domain.SysSchedulePermissions;
import cn.cdeden.knowledge.domain.dto.SysSchedulePermissionsDto;
import cn.cdeden.knowledge.service.SysPermissionsRelevanceService;
import cn.cdeden.knowledge.service.SysScheduleDetailsService;
import cn.cdeden.knowledge.service.SysSchedulePermissionsService;
import cn.cdeden.knowledge.utils.BeanCopyUtil;
import cn.cdeden.system.api.RemoteUserService;
import cn.cdeden.system.api.domain.vo.RemoteUserVo;
import cn.cdeden.system.api.model.LoginUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * *作者：gjx
 * *日期：2024/7/10    11:40
 * *文件描述：
 */
@RestController
//@Api(tags = "日历权限管理")
@RequestMapping("/api/sys/schedule/permissions")
public class SysSchedulePermissionsController {

    @Autowired
    private SysSchedulePermissionsService sysSchedulePermissionsService;

    @Autowired
    private SysPermissionsRelevanceService sysPermissionsRelevanceService;

    @Autowired
    private SysScheduleDetailsService sysScheduleDetailsService;

    @DubboReference
    private RemoteUserService remoteUserService;

    @GetMapping("/get-all")
//    @Log("查询权限")
//    @ApiOperation("查询权限")
    public R<List<SysSchedulePermissionsDto>> getAll() {
//        List<SysSchedulePermissionsDto> allPermissions = sysSchedulePermissionsService.getAllPermissions();
        return R.ok(sysSchedulePermissionsService.getAllPermissions());
    }

    @PostMapping("/add")
//    @Log("新增权限")
//    @ApiOperation("新增权限")
    @Transactional
    public R add(@RequestBody SysSchedulePermissionsDto sysSchedulePermissionsDto) {
        SysSchedulePermissions sysSchedulePermissions = new SysSchedulePermissions();
        BeanCopyUtil.copyProperties(sysSchedulePermissionsDto, sysSchedulePermissions);
        LoginUser user = LoginHelper.getLoginUser();
        sysSchedulePermissions.setCreateBy(user.getUserId());
        List<SysSchedulePermissions> sysSchedulePermissionsList = sysSchedulePermissionsService.list(new QueryWrapper<SysSchedulePermissions>().eq("permissions_name", sysSchedulePermissionsDto.getPermissionsName()));
        if (!sysSchedulePermissionsList.isEmpty()) {
            return R.fail("406","该权限已存在！");
        }
        sysSchedulePermissionsService.save(sysSchedulePermissions);
        List<SysPermissionsRelevance> sysPermissionsRelevanceList = new ArrayList<>();
        sysSchedulePermissionsDto.getDetailsIds().forEach(v -> {
            SysPermissionsRelevance sysPermissionsRelevance = new SysPermissionsRelevance();
            sysPermissionsRelevance.setPermissionsId(sysSchedulePermissions.getPermissionsId());
            sysPermissionsRelevance.setDetailsId(v);
            sysPermissionsRelevanceList.add(sysPermissionsRelevance);
        });
        boolean b = sysPermissionsRelevanceService.saveBatch(sysPermissionsRelevanceList);
        return R.ok(b);
    }

    @GetMapping("/edit/{id}")
//    @Log("修改权限数据回显")
//    @ApiOperation("修改权限数据回显")
    public R<SysSchedulePermissionsDto> edit(@PathVariable Long id) {
        SysSchedulePermissionsDto sysSchedulePermissionsDto = new SysSchedulePermissionsDto();
        List<SysScheduleDetails> sysScheduleDetailsList = new ArrayList<>();
        SysSchedulePermissions sysSchedulePermissions = sysSchedulePermissionsService.getById(id);
        BeanCopyUtil.copyProperties(sysSchedulePermissions, sysSchedulePermissionsDto);
        List<SysPermissionsRelevance> sysPermissionsRelevanceList = sysPermissionsRelevanceService.list(new QueryWrapper<SysPermissionsRelevance>().eq("permissions_id", id));
        List<Long> detailsIdList = sysPermissionsRelevanceList.stream().map(SysPermissionsRelevance::getDetailsId).collect(Collectors.toList());

        if (!detailsIdList.isEmpty()) {
            sysScheduleDetailsList = sysScheduleDetailsService.list(new QueryWrapper<SysScheduleDetails>().select("details_id", "details_name", "`show`").in("details_id", detailsIdList));
        }
        sysSchedulePermissionsDto.setSysScheduleDetailsList(sysScheduleDetailsList);
        RemoteUserVo loginUser = remoteUserService.selectUserByUserId(sysSchedulePermissions.getCreateBy());
        sysSchedulePermissionsDto.setCreateUserName(loginUser.getNickName());
        return R.ok(sysSchedulePermissionsDto);
    }

    @PutMapping("/edit")
//    @Log("修改权限")
//    @ApiOperation("修改权限")
    @Transactional
    public R edit(@RequestBody SysSchedulePermissionsDto sysSchedulePermissionsDto) {
        SysSchedulePermissions sysSchedulePermissions = new SysSchedulePermissions();
        BeanCopyUtil.copyProperties(sysSchedulePermissionsDto, sysSchedulePermissions);
        List<SysSchedulePermissions> sysSchedulePermissionsList = sysSchedulePermissionsService.list(new QueryWrapper<SysSchedulePermissions>()
                .eq("permissions_name", sysSchedulePermissionsDto.getPermissionsName()).ne("permissions_id", sysSchedulePermissionsDto.getPermissionsId()));
        if (!sysSchedulePermissionsList.isEmpty()) {
            return R.fail("406","该权限已存在！");
        }
        boolean b = sysSchedulePermissionsService.updateById(sysSchedulePermissions);
        sysPermissionsRelevanceService.remove(new QueryWrapper<SysPermissionsRelevance>().eq("permissions_id", sysSchedulePermissions.getPermissionsId()));
        List<SysPermissionsRelevance> sysPermissionsRelevanceList = new ArrayList<>();
        sysSchedulePermissionsDto.getDetailsIds().forEach(v -> {
            SysPermissionsRelevance sysPermissionsRelevance = new SysPermissionsRelevance();
            sysPermissionsRelevance.setPermissionsId(sysSchedulePermissionsDto.getPermissionsId());
            sysPermissionsRelevance.setDetailsId(v);
            sysPermissionsRelevanceList.add(sysPermissionsRelevance);
        });
        sysPermissionsRelevanceService.saveBatch(sysPermissionsRelevanceList);
        return R.ok(b);
    }

    @DeleteMapping("/delete/{id}")
    @Transactional
//    @Log("删除权限权限")
//    @ApiOperation("删除权限权限")
    public R delete(@PathVariable Long id) {
        sysSchedulePermissionsService.removeById(id);
        boolean permissionsId = sysPermissionsRelevanceService.remove(new QueryWrapper<SysPermissionsRelevance>().eq("permissions_id", id));
        return R.ok(permissionsId);
    }

    @GetMapping("/get-relevance")
//    @Log("查询详细权限")
//    @ApiOperation("查询详细权限")
    public R<List<SysScheduleDetails>> getRelevance() {
        return R.ok(sysScheduleDetailsService.list(new QueryWrapper<SysScheduleDetails>().select("details_id", "details_name", "`show`").eq("`show`", 1)));
    }
}
